package top.infopub.mgr.hyp.service.impl;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hyp.dao.HyOperatorDao;
import top.infopub.mgr.hyp.dao.HyOperatorRoleDao;
import top.infopub.mgr.hyp.domain.HyOperatorDomain;
import top.infopub.mgr.hyp.service.HyOperatorService;
import top.infopub.mgr.hyp.vo.HyOperatorVo;
import top.infopub.model.hy.BaseInfo;
import top.infopub.model.hy.OperatorRole;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class HyOperatorServiceImpl extends GenericServiceImpl<BaseInfo, String> implements HyOperatorService {

    @Autowired
    private HyOperatorDao operatorDao;

    @Autowired
    private HyOperatorRoleDao operatorRoleDao;

    @Override
    public GenericDao<BaseInfo, String> getDao() {
        return operatorDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOperatorRole(String checkRoleId, String operatorId) {
        operatorRoleDao.deleteByOperatorId(operatorId);
        if (StringUtils.isNotBlank(checkRoleId)) {
            String[] roleIds = checkRoleId.split(",");
            if (roleIds != null && roleIds.length > 0) {
                for (String roleId : roleIds) {
                    OperatorRole operatorRole = new OperatorRole();
                    operatorRole.setSoperatorId(operatorId);
                    operatorRole.setSroleId(roleId);
                    operatorRoleDao.insert(operatorRole);
                }
            }
        }
    }

    @Override
    public Page<HyOperatorDomain> selectPageByDomainWhere(Page<HyOperatorDomain> page,
                                                          HyOperatorVo operatorVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<HyOperatorDomain>)operatorDao.selectByDomainWhere(operatorVo);
    }

}